// Copyright 2010 The Native Client Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can
// be found in the LICENSE file.

#ifndef CALCULATOR_NACL_CALCULATOR_H_
#define CALCULATOR_NACL_CALCULATOR_H_

#include <pthread.h>
#if defined(__native_client__)
#include <nacl/nacl_npapi.h>
#include <nacl/npapi_extensions.h>
#else
#include "third_party/npapi/bindings/npapi.h"
#include "third_party/npapi/bindings/npapi_extensions.h"
#include "third_party/npapi/bindings/nphostapi.h"
#endif
#include <string>
#include <vector>

#include "c_salt/callback.h"
#include "c_salt/module.h"
#include "c_salt/scripting_bridge.h"
#include "c_salt/type.h"

namespace c_salt {
class ObjectType;
}  // namespace c_salt

namespace calculator {

class Calculator : public c_salt::Module {
 public:
  Calculator();
  virtual ~Calculator();

  virtual void InitializeMethods(c_salt::ScriptingBridge* bridge);

  // Unpack the args: args[0] is assumed to be the expression array, in postfix
  // notation.  args[1] is assumed to be the JavaScript function object that
  // is used as the computation result callback.  Once the args are all
  // unpacked, call EvaluateExpression().
  bool Calculate(c_salt::ScriptingBridge* bridge,
                 const NPVariant* args,
                 uint32_t arg_count,
                 NPVariant* return_value);

  // Evaulate |expression| as a statement in postfix notation.  Return the
  // result.
  double EvaluateExpression(const c_salt::Type::TypeArray* expression);

 private:
  c_salt::MethodCallback<Calculator>* calculate_callback_;
  DISALLOW_COPY_AND_ASSIGN(Calculator);
};

}  // namespace calculator

#endif  // CALCULATOR_NACL_CALCULATOR_H_
